﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ScrapySharp.Extensions;

namespace NC.Cather
{
    public static class CatherExtension
    {
        /// <summary>
        /// 删除某标签内部所有Style和Script标签
        /// </summary>
        /// <param name="node">父标签</param>
        /// <returns>删除标签之后的干净标签</returns>
        public static HtmlNode RemoveStylesAndScripts(this HtmlNode node)
        {
            node = RemoveStyles(node);
            return RemoveScripts(node);
        }
        /// <summary>
        /// 删除该标签内部所有Style标签
        /// </summary>
        /// <param name="node">父标签</param>
        /// <returns>删除标签之后的干净标签</returns>
        public static HtmlNode RemoveStyles(this HtmlNode node)
        {
            return RemoveTags(node, "style");
        }
        /// <summary>
        /// 删除该标签内部所有Script标签
        /// </summary>
        /// <param name="node">父标签</param>
        /// <returns>删除标签之后的干净标签</returns>
        public static HtmlNode RemoveScripts(this HtmlNode node)
        {
            return RemoveTags(node, "script");
        }
        /// <summary>
        /// 删除该标签内部所有筛选标签
        /// </summary>
        /// <param name="node">父标签</param>
        /// <param name="selector">筛选规则</param>
        /// <returns>删除标签之后的干净标签</returns>
        public static HtmlNode RemoveTags(this HtmlNode node, string selector)
        {
            foreach (var htmlNode in node.CssSelect(selector))
            {
                htmlNode.Remove();
            }
            return node;
        }
        /// <summary>
        /// 对css样式语法糖扩展
        /// </summary>
        /// <param name="node"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static HtmlNode[] CssSelects(this HtmlNode node, string selector)
        {
            //后期要用正则表达式来进行数据提取

            //对selector进行判断
            string numSelector = string.Empty;
            if (selector.Contains('[') && selector.Contains(']'))
            {
                //对不同接口数据进行构造
                int firstIndex = selector.IndexOf('[');
                numSelector = selector.Substring(firstIndex + 1, selector.Length - 2 - firstIndex);
                selector = selector.Substring(0, firstIndex);
            }
            var nodes = node.CssSelect(selector).ToArray();
            int length = nodes.Length;
            int tempNum = 0,tempNum1;
            if (!string.IsNullOrEmpty(numSelector))
            {
                //开始分析
                if (numSelector.Contains("..."))
                {
                    var splits = numSelector.Split('.').Where(n => !string.IsNullOrEmpty(n)).ToArray();
                    // ...1
                    if (numSelector.StartsWith("..."))
                    {
                        if (Int32.TryParse(splits[0], out tempNum))
                            nodes = nodes.Take(tempNum+1).ToArray();
                        else
                            throw new Exception("...{num} 中num 数据类型出错");
                    }
                    //2...
                    else if (numSelector.EndsWith("..."))
                    {
                        if (Int32.TryParse(splits[0], out tempNum))
                            nodes = nodes.Skip(tempNum).Take(length).ToArray();
                        else
                            throw new Exception("{num}... 中num 数据类型出错");
                    }
                    // 1...2
                    else if (splits.Length == 2 && Int32.TryParse(splits[0], out tempNum) &&
                             Int32.TryParse(splits[1], out tempNum1))
                    {
                        nodes = nodes.Skip(tempNum).Take(tempNum1 - tempNum + 1).ToArray();
                    }
                    else
                        throw new Exception("语法糖格式错误");
                }else if (Int32.TryParse(numSelector, out tempNum))
                {
                    nodes = nodes.Skip(tempNum).Take(1).ToArray();
                }
            }
            return nodes;
        }
    }
}
